#!/usr/bin/env python
#
#       Types.py
#       
#       Copyright 2009 Ben Davis <its.hip@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import string
import random

class UIDDict(dict):
    """Dictionary for holding information which generates a UID"""
    def append(self, y):
        """Generate a random key and add item"""
        while True:
            # Generate a new random key
            print "Generating new key...",
            key = ''.join([random.choice(string.letters) for i in range(4)])
            print key
            # If the key doesn't already exist in the dictionary
            if not self.has_key(key):
                print "Not already in dictionary, adding to the list...",
                # Add the item to the list
                dict.__setitem__(self, key, y)
                y.__key__ = key
                print "OK"
                return
        
    def __setitem__(self, i, y):
        raise KeyError
    
    def get_key(self, item):
        for key, element in self.items():
            if element == item:
                return key
        raise KeyError
